<?php
$pageTitle = '男友列表';
require_once '../../config/config.php';
require_once '../../includes/functions.php';
require_once '../../classes/User.php';
require_once '../../classes/Friend.php';

// 检查用户是否登录
if (!isLoggedIn()) {
    setFlashMessage('error', '请先登录');
    redirect(SITE_URL . '/login.php');
}

$userId = $_SESSION['user_id'];
$friend = new Friend();
$friends = $friend->getFriends($userId);

include '../../includes/header.php';
?>

<div class="container py-4">
    <div class="row">
        <div class="col-lg-10 mx-auto">
            <div class="card shadow-sm">
                <div class="card-header bg-primary text-white">
                    <div class="d-flex flex-sm-row justify-content-between align-items-start align-items-sm-center">
                        <h5 class="mb-2 mb-sm-0"><i class="fas fa-user-friends me-2"></i>我的男友</h5>
                        <div class="d-flex flex-wrap gap-2">
                            <a href="<?php echo SITE_URL; ?>/views/friend/request.php" class="btn btn-light btn-sm">
                                <i class="fas fa-user-plus me-1"></i><span class="d-none d-sm-inline"></span>男友请求
                            </a>
                            <a href="<?php echo SITE_URL; ?>/views/friend/search.php" class="btn btn-light btn-sm">
                                <i class="fas fa-search me-1"></i><span class="d-none d-sm-inline"></span>查找男友
                            </a>
                        </div>
                    </div>
                </div>
                <div class="card-body" style="display: flex;flex-direction: row;flex-wrap: wrap;justify-content: center;">
                    <?php if (isset($_SESSION['flash_message'])): ?>
                        <div class="alert alert-<?php echo $_SESSION['flash_type']; ?> alert-dismissible fade show">
                            <?php echo $_SESSION['flash_message']; ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                        </div>
                        <?php unset($_SESSION['flash_message'], $_SESSION['flash_type']); ?>
                    <?php endif; ?>
                    
                    <?php if (empty($friends)): ?>
                        <div class="text-center py-5">
                            <i class="fas fa-users fa-3x text-muted mb-3"></i>
                            <p class="lead">您还没有男友</p>
                        </div>
                    <?php else: ?>
                        <div class="friend-list row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3">
                            <?php foreach ($friends as $friend): ?>
                                <div class="col">
                                    <div class="friend-card p-3">
                                        <div class="d-flex">
                                            <div class="flex-shrink-0">
                                                <img src="<?php echo getUserAvatar($friend['avatar']); ?>" class="rounded-circle avatar-md" alt="<?php echo htmlspecialchars($friend['nickname'] ?: $friend['username']); ?>">
                                            </div>
                                            <div class="ms-3 flex-grow-1">
                                                <h5 class="mb-1"><?php echo htmlspecialchars($friend['nickname'] ?: $friend['username']); ?></h5>
                                                <div class="d-flex align-items-center mb-2">
                                                    <div class="text-warning me-2">
                                                        <i class="fas fa-heart"></i>
                                                    </div>
                                                    <div class="progress flex-grow-1" style="height: 6px;">
                                                        <div class="progress-bar bg-warning" role="progressbar" style="width: <?php echo min(100, ($friend['intimacy'] / 100) * 100); ?>%" aria-valuenow="<?php echo $friend['intimacy']; ?>" aria-valuemin="0" aria-valuemax="100"></div>
                                                    </div>
                                                    <span class="ms-2 small"><?php echo $friend['intimacy']; ?></span>
                                                </div>
                                                <div class="friend-actions mt-2">
                                                    <a href="<?php echo SITE_URL; ?>/views/friend/profile.php?id=<?php echo $friend['id']; ?>" class="btn btn-sm btn-outline-primary me-1 mb-1">
                                                        <i class="fas fa-user me-1"></i><span class="d-none d-md-inline"></span>查看
                                                    </a>
                                                    <a href="<?php echo SITE_URL; ?>/views/task/create.php?friend_id=<?php echo $friend['id']; ?>" class="btn btn-sm btn-outline-success me-1 mb-1">
                                                        <i class="fas fa-calendar-plus me-1"></i><span class="d-none d-md-inline"></span>约定
                                                    </a>
                                                    <button type="button" class="btn btn-sm btn-outline-danger mb-1 delete-friend" data-id="<?php echo $friend['id']; ?>" data-name="<?php echo htmlspecialchars($friend['nickname'] ?: $friend['username']); ?>">
                                                        <i class="fas fa-trash-alt me-1"></i><span class="d-none d-md-inline"></span>删除
                                                    </button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 删除男友确认模态框 -->
<div class="modal fade" id="deleteFriendModal" tabindex="-1" aria-labelledby="deleteFriendModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="deleteFriendModalLabel">确认删除男友</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p>您确定要删除 <span id="friendName"></span> 吗？</p>
                <p class="text-danger">删除后，您将失去与该男友的所有亲密度记录。</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <form id="deleteFriendForm" method="POST" action="<?php echo SITE_URL; ?>/api/friend/delete.php">
                    <input type="hidden" name="friend_id" id="friendId">
                    <button type="submit" class="btn btn-danger">确认删除</button>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    // 删除男友确认
    const deleteBtns = document.querySelectorAll('.delete-friend');
    const friendNameEl = document.getElementById('friendName');
    const friendIdInput = document.getElementById('friendId');
    
    deleteBtns.forEach(btn => {
        btn.addEventListener('click', function() {
            const id = this.dataset.id;
            const name = this.dataset.name;
            
            friendNameEl.textContent = name;
            friendIdInput.value = id;
            
            const modal = new bootstrap.Modal(document.getElementById('deleteFriendModal'));
            modal.show();
        });
    });
});
</script>

<?php include '../../includes/footer.php'; ?>
